namespace CIS.Customer
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CustomerInfoBasicSearch : Page
    {
        protected TextBox CompanyNameTbx;
        protected DropDownList CustomerLevelDdl;
        protected TextBox CustomerNameTbx;
        protected TextBox CustomerTitleTbx;
        protected Image iSearch;
        protected Label lbBasicInfo;
        protected Label lbCompanyName;
        protected Label lbCustomerLevel;
        protected Label lbCustomerName;
        protected Label lbCustomerTitle;
        protected UserPrincipal Principal = null;
        protected Button searchBtn;

        private void InitializeComponent()
        {
            this.searchBtn.Click += new EventHandler(this.searchBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.CustomerLevelDdl.Items.Clear();
            for (int i = 0; i <= 5; i++)
            {
                string tmpStr = "Level " + i.ToString();
                ListItem tmpItemLevel = new ListItem();
                tmpItemLevel.Text = tmpStr;
                tmpItemLevel.Value = i.ToString();
                this.CustomerLevelDdl.Items.Add(tmpItemLevel);
            }
            this.CustomerLevelDdl.Items[0].Text = "";
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            this.Page.Session.Add("CompanyName", this.CompanyNameTbx.Text.Trim());
            this.Page.Session.Add("CustomerName", this.CustomerNameTbx.Text.Trim());
            this.Page.Session.Add("CustomerTitleTbx", this.CustomerTitleTbx.Text.Trim());
            this.Page.Session.Add("CustomerLevel", this.CustomerLevelDdl.SelectedValue);
            base.Response.Redirect("~/CIS/Customer/CustomerInfoBasicListForSearch.aspx?IsSearch=True", true);
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeA == 0)
            {
                this.InitPage();
            }
            else if (this.Principal.OwnRole.TypeA == 1)
            {
                this.InitPage();
            }
            else if (this.Principal.OwnRole.TypeA == 2)
            {
                this.searchBtn.Visible = false;
                this.CustomerNameTbx.Visible = false;
                this.CustomerTitleTbx.Visible = false;
                this.CompanyNameTbx.Visible = false;
                this.lbCompanyName.Visible = false;
                this.lbCustomerName.Visible = false;
                this.lbCustomerTitle.Visible = false;
                this.lbBasicInfo.Visible = false;
                this.CustomerLevelDdl.Visible = false;
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbCompanyName.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoSearchPage", "lbCompanyName", language, path);
            this.lbCustomerName.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoSearchPage", "lbCustomerName", language, path);
            this.lbCustomerTitle.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoSearchPage", "lbCustomerTitle", language, path);
            this.searchBtn.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoSearchPage", "searchBtn", language, path);
            this.lbCustomerLevel.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoSearchPage", "lbCustomerLevel", language, path);
            this.lbBasicInfo.Text = ManageSystemConfig.GetSystemSetLanguage("CustomerInfoSearchPage", "lbBasicInfo", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iSearch.ImageUrl = "../images/Title_Search.jpg";
                    break;

                case "en":
                    this.iSearch.ImageUrl = "../images/Title_SearchEn.jpg";
                    break;
            }
        }
    }
}

